package com.shop.cereshop.agent.controller.userAuth;
import cn.hutool.core.bean.BeanUtil;
import com.shop.cereshop.agent.page.user.CereAgentUserVO;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.agent.CereAgentRealnameAuth;
import com.shop.cereshop.commons.domain.agent.CereAgentRole;
import com.shop.cereshop.commons.domain.user.faceCheck.FaceCheckModelTwo;
import com.shop.cereshop.commons.domain.user.faceCheck.respond.FaceCheckRespondTwo;
import com.shop.cereshop.commons.domain.userAuth.UserRealnameAuthRespond;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.agent.service.FaceCheckService.FaceCheckServiceTwo;
import com.shop.cereshop.agent.service.agentAuth.AgentRealnameAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/realname")
@Slf4j(topic = "BusinessController")
@Api(value = "代理商实名认证", tags = "代理商实名接口")
public class UserRealnameAuthController  {
    @Autowired
    private FaceCheckServiceTwo faceCheckService;

    @Autowired
    private AgentRealnameAuthService agentRealnameAuthService;

    @PostMapping("userAuth")
    @ApiOperation(value = "实名认证")
    public Result<String> userAuth(@RequestBody FaceCheckModelTwo model, HttpServletRequest request) throws CoBusinessException, Exception {

        try {

            // 解密请求参数
//            model = model.decrypt();
        } catch (Exception e) {
            return new Result<>(CoReturnFormat.PARAM_INVALID);
        }
        String token = request.getHeader("Authorization_Agent");
        CereAgentUserVO cereAgentUserVO=null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询代理商信息
            cereAgentUserVO = (CereAgentUserVO)request.getAttribute("user");
        }
//        查询该代理商是否实名认证过
        CereAgentRealnameAuth cereAgentRealnameAuth = agentRealnameAuthService.selectByIdCartNumber(model.getIdCardNumber());
        if (cereAgentRealnameAuth != null){
            return new Result<String>("该用户已实名认证", "5001");
        }

        FaceCheckRespondTwo faceCheckRespondTwo = faceCheckService.faceCheckTwo(model);

        if ("200".equals(faceCheckRespondTwo.getCode())&& "1".equals(faceCheckRespondTwo.getResultObject().getBizCode())) {
            Integer i = agentRealnameAuthService.saveUserAuth(cereAgentUserVO, faceCheckRespondTwo, model,cereAgentUserVO.getPhone());
            if (i>0) {
                return new Result<String>("success", CoReturnFormat.SUCCESS);
            }
        }
        return new Result<String>("认证失败 人像不符", "5002");
    }

    @GetMapping("getAuthInfo")
    @ApiOperation(value = "获取实名认证信息")
    public Result<UserRealnameAuthRespond> getAuthInfo(Long userId, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
       CereAgentRealnameAuth cereAgentRealnameAuth = null;
        UserRealnameAuthRespond byUserId = agentRealnameAuthService.findAuthInfoByUserId(userId);
        if (BeanUtil.isNotEmpty(byUserId)) {
            return new Result<UserRealnameAuthRespond>(byUserId, CoReturnFormat.SUCCESS);
        }
        return new Result<UserRealnameAuthRespond>("未查询到认证信息", "5001");
    }
}
